package com.asen.business.commons.controller;

import com.asen.business.commons.domain.CoreBill;
import com.asen.business.commons.service.CoreBillService;
import com.asen.commons.annotation.Lock;
import com.asen.commons.annotation.SkipAuthorization;
import com.asen.commons.constants.BaseConstant;
import com.asen.commons.core.base.result.ApiResult;
import com.asen.commons.core.meta.dao.Condition;
import com.asen.commons.core.utils.EntityUtils;
import com.asen.commons.utils.BaseAssert;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 业务单据Controller
 *
 * @author Gary
 * @version 1.0
 * @date 2018-12-05
 */
public abstract class AbstractCoreBillController<T extends CoreBill, D, V, S extends CoreBillService<T>> extends AbstractBillController<T, D, V, S> {

    @SkipAuthorization
    @ApiOperation(value = "复制创建", httpMethod = BaseConstant.API_GET_METHOD)
    @RequestMapping(value = "/copyCreate")
    public ApiResult<V> copyCreate(@RequestParam("id") Long id) throws Exception {
        V result = null;
        try {
            BaseAssert.notNull(id, "参数id不能为空");
            T model = getService().get(id);
            if (model != null) {
                EntityUtils.clearData(model);
                result = newVo();
                buildVo(model, result);
            }
        } catch (Exception e) {
            logger.error("复制失败", e);
            return fail(e.getMessage());
        }
        return success(result);
    }

    @Lock
    @ApiOperation(value = "提交", httpMethod = BaseConstant.API_POST_METHOD)
    @PostMapping(value = "/submit")
    public ApiResult<Long> submit(@RequestBody D dto) throws Exception {
        T model;
        BaseAssert.notNull(dto, "参数不能为空");
        if (getDoClass().equals(getDtoClass())) {
            model = (T) dto;
        } else {
            model = newModel();
        }
        submitValidate(dto);
        buildSubmit(model, dto);
        doSubmit(model);
        return success(model.getId(), "提交成功");
    }

    @Lock
    @ApiOperation(value = "审核", httpMethod = BaseConstant.API_POST_METHOD)
    @PostMapping(value = "/audit")
    public ApiResult<String> audit(Long id) throws Exception {
        BaseAssert.notEmpty(id, "参数不能为空");
        doAudit(id);
        return success("审核成功");
    }

    @Lock
    @ApiOperation(value = "反审核", httpMethod = BaseConstant.API_POST_METHOD)
    @PostMapping(value = "/unAudit")
    public ApiResult<String> unAudit(Long id) throws Exception {
        BaseAssert.notEmpty(id, "参数不能为空");
        doUnAudit(id);
        return success("反审核成功");
    }

    @SkipAuthorization
    @ApiOperation(value = "获取ID", httpMethod = BaseConstant.API_GET_METHOD)
    @RequestMapping(value = "/getId")
    public ApiResult<Long> getId(String number) throws Exception {
        BaseAssert.notEmpty(number, "请输入单据编号");
        Condition condition = new Condition();
        condition.select("id", "number");
        condition.equal("number", number);
        T model = getService().getBy(condition);
        // BaseAssert.notNull(model, "请输入正确的单据编号");
        if (model == null) {
            return fail("请输入正确的单据编号");
        }
        Long result = model.getId();
        return success(result);
    }

    protected void submitValidate(D dto) throws Exception {

    }

    protected void buildSubmit(T model, D dto) throws Exception {
        buildModel(model, dto);
    }

    protected void doSubmit(T model) throws Exception {
        getService().submit(model);
    }

    protected void doAudit(Long id) throws Exception {
        getService().audit(id);
    }

    protected void doUnAudit(Long id) throws Exception {
        getService().unAudit(id);
    }
}
